<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>转换字符的编码</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.mb-convert-case.html">« mb_convert_case</a></li>
      <li style="float: right;"><a href="function.mb-convert-kana.html">mb_convert_kana »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.mbstring.html">多字节字符串 函数</a></li>
    <li>转换字符的编码</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mb-convert-encoding" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mb_convert_encoding</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">mb_convert_encoding</span> &mdash; <span class="dc-title">转换字符的编码</span></p>

 </div>
   
 <div class="refsect1 description" id="refsect1-function.mb-convert-encoding-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mb_convert_encoding</strong></span>(<span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">string</span></span> <code class="parameter">$string</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$to_encoding</code></span>, <span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">string</span>|<span class="type">null</span></span> <code class="parameter">$from_encoding</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type">array</span>|<span class="type">string</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   将 <span class="type">string</span> 类型 <code class="parameter">str</code> 的字符编码从可选的 <code class="parameter">from_encoding</code> 转换到 <code class="parameter">to_encoding</code>。
   当参数 <code class="parameter">string</code> 是一个 <span class="type">array</span> 时，将递归转换它所有的 <span class="type">string</span> 值。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mb-convert-encoding-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       要编码的 <span class="type">string</span> 或 <span class="type">array</span>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">to_encoding</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">string</code> 要转换成的编码类型。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">from_encoding</code></dt>

     <dd>

      <p class="para">
       在转换前通过字符代码名称来指定。它可以是一个 <span class="type">array</span> 也可以是逗号分隔的枚举列表。
       如果没有提供 <code class="parameter">from_encoding</code>，则会使用内部（internal）编码。
       
      </p>
      <p class="para">
      参见<a href="mbstring.supported-encodings.html" class="link">支持的编码</a>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.mb-convert-encoding-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   编码后的 <span class="type">string</span>。
   成功时返回编码后的 <span class="type">string</span> 或 <span class="type">array</span>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.mb-convert-encoding-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   当 <code class="parameter">to_encoding</code> 或 <code class="parameter">from_encoding</code> 为无效的编码时，
   PHP 8.0.0 起将抛出 <span class="classname"><a href="class.valueerror.html" class="classname">ValueError</a></span>；
   而在 PHP 8.0.0 之前的版本里，会产生一个 <strong><code>E_WARNING</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.mb-convert-encoding-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       现在，当 <code class="parameter">to_encoding</code> 为无效编码时，
       <span class="function"><strong>mb_convert_encoding()</strong></span> 会抛出 <span class="classname"><a href="class.valueerror.html" class="classname">ValueError</a></span>。
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       现在，当 <code class="parameter">from_encoding</code> 为无效编码时，
       <span class="function"><strong>mb_convert_encoding()</strong></span> 会抛出 <span class="classname"><a href="class.valueerror.html" class="classname">ValueError</a></span>。
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       现在 <code class="parameter">from_encoding</code> 可以传入 null。
      </td>
     </tr>

     <tr>
      <td>7.2.0</td>
      <td>
       现在该函数的 <code class="parameter">string</code> 参数同时能接受 <span class="type">array</span> 类型。
       在此之前，仅支持 <span class="type">string</span>。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.mb-convert-encoding-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2560">
    <p><strong>示例 #1 <span class="function"><strong>mb_convert_encoding()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;转换内部编码为&nbsp;SJIS&nbsp;*/<br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mb_convert_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SJIS"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;将&nbsp;EUC-JP&nbsp;转换成&nbsp;UTF-7&nbsp;*/<br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mb_convert_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"UTF-7"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"EUC-JP"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;从&nbsp;JIS,&nbsp;eucjp-win,&nbsp;sjis-win&nbsp;中自动检测编码，并转换&nbsp;str&nbsp;到&nbsp;UCS-2LE&nbsp;*/<br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mb_convert_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"UCS-2LE"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"JIS,&nbsp;eucjp-win,&nbsp;sjis-win"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;"auto"&nbsp;扩展成&nbsp;"ASCII,JIS,UTF-8,EUC-JP,SJIS"&nbsp;*/<br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mb_convert_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"EUC-JP"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"auto"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

  
 <div class="refsect1 seealso" id="refsect1-function.mb-convert-encoding-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.mb-detect-order.html" class="function" rel="rdfs-seeAlso">mb_detect_order()</a> - 设置/获取 字符编码的检测顺序</span></li>
   </ul>
  </p>
 </div>

 
</div></div></div></body></html>